import { Product, Purchase, Subscription } from 'react-native-iap';

/**
 * 支付流程关键的事件回调函数：
 */
export interface PaymentEventCallbacks {
  // 创建业务订单成功
  onCreateOrderSuccess?: (order: OrderResult) => void;
  // 创建业务订单失败
  onCreateOrderFailed?: (error: Error) => void;
  // 拉起SDK支付界面并支付成功
  onRequestPurchaseSuccess?: (purchase: Purchase | null) => void;
  // 拉起SDK支付界面或支付界面支付失败
  onRequestPurchaseFailed?: (error: Error) => void;
  // 拉起SDK支付界面取消
  onRequestPurchaseCanceled?: () => void;
  // 订阅恢复成功(仅ios)
  onRestoreSuccess?: (result: RestoreResult) => void;
  // 订阅恢复失败(仅iOS)
  onRestoreFailed?: (error: Error) => void;
}

/**
 * 支付服务配置：
 * 发起API服务、环境变量、APP名称、事件钩子
 */
export interface PaymentConfig {
  api: {
    ios: {
      // 创建订单接口
      createOrder: (params: CreateOrderParams) => Promise<Payment.PaymentResult>;
      // 校验支付订单接口
      verifyPurchase: (
        product: Product,
        purchase: Purchase,
        orderId: number,
        googleProductCode: string,
        productCode: string,
      ) => Promise<boolean>;
      // 订阅恢复接口
      restoreSubscription: (transactionReceipt: string) => Promise<RestoreResult>;
    };
    android: {
      // 创建订单接口
      createOrder: (params: CreateOrderParams) => Promise<Payment.PaymentResult>;
      // 校验支付订单接口
      verifyPurchase: (
        product: Product,
        purchase: Purchase,
        orderId: number,
        googleProductCode: string,
        productCode: string,
      ) => Promise<boolean>;
      // 订阅恢复接口
      restoreSubscription?: (transactionReceipt: string) => Promise<RestoreResult>;
    };
  };
  options: {
    // 支付环境
    environment: 'test' | 'prod';
    // 应用名称
    appName: string;
  };
  // 支付流程事件回调
  events?: PaymentEventCallbacks;
}

/**
 * appName: app名字
 * payMethod: 支付方式，支付方式
 * 1 苹果 2 谷歌 3 支付宝扫码支付 10 微信小程序支付 11 微信公众号支付
 * appAccountToken: app账户token,苹果支付唯一id
 * productId: 商品id,商品ID与商品编码填其一
 * productCode: 商品编码,商品编码与商品ID填其一
 * quantity: 数量
 */
export interface CreateOrderParams {
  payMethod: '1' | '2' | '3' | '10' | '11';
  quantity: number;
  productCode: string;
  appAccountToken?: string;
  appName: string;
}

export interface OrderResult {
  code: number;
  msg?: string;
  data: number; // orderId
}

export interface VerifyResult {
  code: number;
  msg: string;
  data: boolean;
}

/**
 * 查询应用交易完成结果
 */
type PaymentQueryResult = {
  /** @description  支付状态: 0待支付、1支付成功、2支付失败 3 订单超时 4 支付中 5 订单关闭 6 支付中超时 */
  status: 0 | 1 | 2 | 3 | 4 | 5 | 6;
  /** @description  退款状态 -1未退款，0待退款，1退款中，2退款完成 */
  refundStatus: -1 | 0 | 1 | 2;
  /** @description  是否发放会员 0 未发放 1 发放 */
  isReceived: 0 | 1;
  /** @description  是否撤销 */
  isRevoked: 0 | 1;
  /** @description  退款单号 */
  refundId?: number;
};

export interface ProductListResult {
  code: number;
  msg?: string;
  data: Array<Product>;
}

export interface RestoreResult {
  code: number;
  msg: string;

  data: unknown;
}

export interface PurchaseParams {
  product: Product | Subscription;
  skus: string[];
  appAccountToken?: string;
  orderId?: string | number;
  userId?: string;
  env: 'test' | 'prod';
  businessProductCode: string;
}

export interface PaymentResult {
  success: boolean;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  data?: any;
  error?: Error;
}
